﻿using System.Windows;
using System.Windows.Navigation;
using GalaSoft.MvvmLight.Threading;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WinSource.ViewModel;

namespace WinSource
{
    public partial class App
    {
        #region Fields

        private bool _isPhoneApplicationInitialized;
        private static string _version;

        #endregion

        #region Constructors

        public App()
        {
            //BugSenseHandler.Instance.Init(this, "d17bb886");
            UnhandledException += ApplicationUnhandledException;

            InitializeComponent();
            InitializePhoneApplication();

            TiltEffect.SetIsTiltEnabled(RootFrame, true);
            
            DispatcherHelper.Initialize();
        }

        #endregion

        #region Properties

        public PhoneApplicationFrame RootFrame { get; private set; }

        public static string Version
        {
            get
            {
                if (_version == null)
                {
                    try
                    {
                        string assemblyName = typeof (App).Assembly.ToString();

                        // ReSharper disable StringIndexOfIsCultureSpecific.1
                        if (assemblyName.IndexOf("Version=") >= 0)
                        {
                            assemblyName = assemblyName.Substring(assemblyName.IndexOf("Version=") + "Version=".Length);
                            assemblyName = assemblyName.Substring(0, assemblyName.IndexOf(","));

                            string[] versions = assemblyName.Split('.');

                            _version = string.Format("{0}.{1}", versions[0], versions[1]);
                        }
                        // ReSharper restore StringIndexOfIsCultureSpecific.1
                    }
                    catch
                    {
                    }
                }

                return _version;
            }
        }

        #endregion

        #region Event Handlers

        private void ApplicationLaunching(object sender, LaunchingEventArgs e)
        {
            ViewModelLocator viewModelLocator = Current.Resources["Locator"] as ViewModelLocator;
            
            if (viewModelLocator != null)
            {
                viewModelLocator.DataService.Load();
            }
        }

        private void ApplicationActivated(object sender, ActivatedEventArgs e)
        {
            if (!e.IsApplicationInstancePreserved)
            {
                ViewModelLocator viewModelLocator = Current.Resources["Locator"] as ViewModelLocator;

                if (viewModelLocator != null)
                {
                    viewModelLocator.DataService.Load();
                }
            }
        }

        private void ApplicationDeactivated(object sender, DeactivatedEventArgs e)
        {
            ViewModelLocator viewModelLocator = Current.Resources["Locator"] as ViewModelLocator;

            if (viewModelLocator != null)
            {
                viewModelLocator.Cleanup();
            }
        }

        private void ApplicationClosing(object sender, ClosingEventArgs e)
        {
        }

        private static void OnRootFrameNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                System.Diagnostics.Debugger.Break();
            }
        }

        private void OnCompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
        {
            RootVisual = RootFrame;

            RootFrame.Navigated -= OnCompleteInitializePhoneApplication;
        }

        private static void ApplicationUnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                System.Diagnostics.Debugger.Break();
            }
        }

        #endregion

        #region Private Methods

        private void InitializePhoneApplication()
        {
            if (!_isPhoneApplicationInitialized)
            {
                RootFrame = new PhoneApplicationFrame();
                RootFrame.Navigated += OnCompleteInitializePhoneApplication;
                RootFrame.NavigationFailed += OnRootFrameNavigationFailed;

                _isPhoneApplicationInitialized = true;
            }
        }

        #endregion
    }
}